package com.Controller;

import com.Service.UploadBookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
import java.io.IOException;
import java.sql.SQLException;

@Controller
@RequestMapping("/modelAuthor")
public class AuthorController {
    @Autowired
    private UploadBookService uploadBookService;
    @PostMapping("/uploadBook")
    @ResponseBody
    public void uploadBook(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, SQLException {
        request.setCharacterEncoding("UTF-8");
        System.out.println(getSubmittedFileName(request.getPart("myFile")));
       uploadBookService.uploadBook(request,response);
    }
    private String getSubmittedFileName(Part part) {
        String header = part.getHeader("content-disposition");
        for (String content : header.split(";")) {
            if (content.trim().startsWith("filename")) {
                return content.substring(content.indexOf('=') + 1).trim().replace("\"", "");
            }
        }
        return null;
    }
}
